#include "stdafx.h"
#include "HBoxes.h"
#include "HGlobalManager.h"

namespace OSLib
{
	template<> BombBox::BoxesManager* Singleton<BombBox::BoxesManager>::ms_Singleton = NULL;
}

namespace BombBox
{
	BoxItem::BoxItem( GameItem_Box* pBox )
		: m_pBox(pBox)
		, m_nLife(0)
		, m_vPos(0,0,0)
		, m_pItemController(NULL)
	{
	}

	BoxItem::~BoxItem( void )
	{
	}

	int BoxItem::getType()
	{
		if( m_pBox == NULL ) return 0;
		return m_pBox->getID();
	}

	const char* BoxItem::getItemName( void )
	{
		return NULL;
	}

	void BoxItem::setItemController( ItemController* pItemController )
	{
		m_pItemController = pItemController;
	}

	irr::core::stringc BoxItem::getItemMesh( int nStatus )
	{
		return m_pBox->getStatusFile( nStatus );
	}

	irr::core::stringc BoxItem::getItemPath( int nStatus )
	{
		irr::core::stringc s = m_pBox->getFilePath();

		return m_pBox->getFilePath();
	}
	
	bool BoxItem::getStatusAnimation( int nStatus )
	{
		return false;
	}

	bool BoxItem::enableLight( int nStatus )
	{
		return false;
	}
	
	void BoxItem::onAnimationStop( int nStatus )
	{
	}

	int BoxItem::getAnimationSpeed( int nStatus )
	{
		return 0;
	}

	bool BoxItem::doDamage( int nDamage )
	{
		m_nLife -= nDamage;

		return m_nLife <= 0;
	}

	//=================================================================

	GameItem_Box::GameItem_Box(void)
	{
		m_sFilePath = "";
	}

	GameItem_Box::~GameItem_Box(void)
	{
	}

	void GameItem_Box::onItemPicked( void )
	{
	}

	void GameItem_Box::addStatus( int nValue, irr::core::stringc sFile )
	{
		m_vBoxStatus[nValue] = sFile;
	}

	BoxItem* GameItem_Box::createInstance( void )
	{
		BoxItem* newBox = new BoxItem( this );
		newBox->m_nLife = m_nLife;
		return newBox;
	}

	void GameItem_Box::setLife( int nlife )
	{
		m_nLife = nlife;
	}

	void GameItem_Box::setPath( irr::core::stringc sPath )
	{
		m_sFilePath = sPath;
	}

	void GameItem_Box::setID( int nID )
	{
		m_nID = nID;
	}

	int GameItem_Box::getID( void )
	{
		return m_nID;
	}

	irr::core::stringc GameItem_Box::getFilePath( void )
	{
		return m_sFilePath;
	}

	irr::core::stringc GameItem_Box::getStatusFile( int nStatus )
	{
		irr::core::map<int,irr::core::stringc>::Node* pNode = m_vBoxStatus.find(nStatus);

		if( pNode == NULL ) return "";

		return pNode->getValue();
	}
	///==============================================================================
	BoxesManager::BoxesManager()
	{
	}

	BoxesManager::~BoxesManager()
	{
	}

	GameItem_Box* BoxesManager::registerBox( int nTypeID )
	{
		if( m_vBoxes.find(nTypeID) == 0 )
		{
			m_vBoxes[ nTypeID ] = new GameItem_Box();
		}
			
		return m_vBoxes[nTypeID];
	}

	BoxItem* BoxesManager::createBox( int nTypeID )
	{
		irr::core::map<int,GameItem_Box*>::Node* v = m_vBoxes.find(nTypeID);
		if( v == 0 ) return NULL;

		return v->getValue()->createInstance();
	}

	void BoxesManager::loadConfig( irr::core::stringc sFile )
	{
		irr::io::IFileSystem* fileSys = s_pGlobalManager->getFileSystem();
		irr::io::IXMLReader* pReader = fileSys->createXMLReader( sFile );

		GameItem_Box* pBoxItem = NULL;

		while(pReader && pReader->read())
		{
			switch(pReader->getNodeType())
			{
			case irr::io::EXN_ELEMENT:
				{
					if(irr::core::stringw("Box") == pReader->getNodeName())
					{
						pBoxItem = NULL;

						int nID = pReader->getAttributeValueAsInt(L"ID");
						irr::core::stringc sFilePath = pReader->getAttributeValue(L"FilePath");
						int nLife = pReader->getAttributeValueAsInt(L"Life");

						if( nID <=0 || nLife <= 0 ) continue;
						
						pBoxItem = registerBox(nID);

						pBoxItem->setLife( nLife );
						pBoxItem->setID( nID );
						pBoxItem->setPath( sFilePath );
					}

					if(irr::core::stringw("Status") == pReader->getNodeName())
					{
						if( pBoxItem == NULL ) continue;

						int nBoxValue = pReader->getAttributeValueAsInt(L"Value");
						irr::core::stringc sBoxFile = pReader->getAttributeValue(L"File");
						
						pBoxItem->addStatus( nBoxValue, sBoxFile );
					}
					break;
				}
			}
		}
	}
}